home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Utilities / RandomDot 1.0.2 / Random Dot Src ƒ / _c / ShowProgress.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-19  |  4.1 KB  |  178 lines  |  [TEXT/KAHL]

  1. /* ShowProgress.c - 
  2.     by David Phillip Oster October 1994 oster@netcom.com
  3.     for:
  4.     Stuart Inglis singlis@waikato.ac.nz
  5.     Department of Computer Science
  6.     University of Waikato, Hamilton, New Zealand
  7.  */
  8. #include <QDOffscreen.h>
  9. #include "RandomDotMain.h"
  10. #include "RandomDotRes.h"
  11. #include "RandomDotWin.h"
  12.  
  13. #include "Progress.h"
  14.  
  15. #include "Error.h"
  16. #include "Utils.h"
  17.  
  18. typedef struct ProgressRec {
  19.     unsigned long    start;
  20.     unsigned long    last;
  21.     Integer            width;
  22.     DialogPtr        win;
  23.     Rect            rFullBar;
  24.     Rect            rBar;
  25.     Str255            labelText;
  26. }ProgressRec;
  27.  
  28. static ProgressRec progress;
  29.  
  30. /* SetProgressLabelText
  31.  */
  32. void SetProgressLabelText(StringPtr s){
  33.     StrMove(s, progress.labelText);
  34. }
  35.  
  36. /* DrawProgress - draw the progress item.
  37.  */
  38. static pascal void DrawProgress(DialogPtr dp, Integer i){
  39.     GrafPtr    savePort;
  40.     Rect    r;
  41.  
  42.     GetPort(&savePort);
  43.     SetPort(dp);
  44.     GetDIRect(i, &progress.rFullBar);
  45.     FrameRect(&progress.rFullBar);
  46.     progress.rBar = progress.rFullBar;
  47.     InsetRect(&progress.rBar, 2, 2);
  48.     r = progress.rBar;
  49.     r.right = r.left + progress.width;
  50.     if(progress.width > 0){
  51.         PaintRect(&r);
  52.     }
  53.     SetPort(savePort);
  54. }
  55.  
  56. /* ProgressWidth - current width
  57.  */
  58. static Integer ProgressWidth(LongInt n, LongInt max){
  59.     return (progress.rBar.right - progress.rBar.left - 2)*n / max;
  60. }
  61.  
  62. /* UpdateProgress - draw just the changed part
  63.  */
  64. static void UpdateProgress(Integer newWidth){
  65.     GrafPtr    savePort;
  66.     Rect    r;
  67.  
  68.     r = progress.rBar;
  69.     r.right = r.left + newWidth;
  70.     r.left += progress.width;
  71.     GetPort(&savePort);
  72.     SetPort(progress.win);
  73.     PaintRect(&r);
  74.     SetPort(savePort);
  75.  
  76.     progress.width = newWidth;
  77. }
  78.  
  79.  
  80. /* ShowProgress - 
  81.     caller calls us, with n ranging from 0 to max.
  82.     we measure the rate at which n goes from 0 to max/10, and do nothing for
  83.     1/2 second. if whole thing will take less than 3 seconds, we do nothing,
  84.     else we put up progress meter with cancel button.
  85.     
  86.     Call this with n == max to reset it.
  87.  */
  88. OSErr    ShowProgress(LongInt n, LongInt max){
  89.     OSErr        errCode;
  90.     unsigned long now;
  91.     static UserItemUPP    drawprogressUPP = NIL;
  92.     Rect        **rh;
  93.     Integer    newWidth;
  94.     EventRecord e;
  95.     Integer        item;
  96.     DialogPtr    dp;
  97.     CGrafPtr    savePort;
  98.     GDHandle    saveGD;
  99.  
  100.     errCode = noErr;
  101.     now = TickCount();
  102.     if(0 == n){
  103.         progress.start = TickCount();
  104.     }else if(n == max){
  105.         if(NIL != progress.win){
  106.             HideWindow(progress.win);
  107.             progress.width = 0;
  108.         }
  109.     }else if(now == progress.last){
  110.         /* done */
  111.     }else{
  112.         GetGWorld(&savePort, &saveGD);
  113.         progress.last = now;
  114.         if((NIL == progress.win || NOT ((WindowPeek) progress.win)->visible) && 
  115.             now - progress.start > 20 && (n*100L)/max < 10){
  116.  
  117.             if(NIL == progress.win){
  118.                 progress.win = GetNewDialog(rProgress, NIL, (WindowPtr) -1L);
  119.                 SetPort(progress.win);
  120.                 if(NIL == drawprogressUPP){
  121.                     drawprogressUPP = NewUserItemProc(DrawProgress);
  122.                 }
  123.                 SetDIHandle(1, (Handle) drawprogressUPP);
  124.                 if(NIL != (rh = (Rect **) GetPreferencesHandle('Wind', 128))){
  125.                     if(TitleBarOnScreen(*rh)){    
  126.                         MoveWindow(progress.win, (**rh).left, (**rh).top, FALSE);
  127.                     }
  128.                     DisposeHandle((Handle) rh);
  129.                 }
  130.             }
  131.             SetIText(GetWDIHandle(progress.win, 3), progress.labelText);
  132.             ShowWindow(progress.win);
  133.             SelectWindow(progress.win);
  134.             DrawDialog(progress.win);
  135.         }else if(NIL != progress.win && ((WindowPeek) progress.win)->visible){
  136.             newWidth = ProgressWidth(n, max);
  137.             if(newWidth != progress.width){
  138.                 UpdateProgress(newWidth);
  139.                 WaitNextEvent(everyEvent, &e, NIL, 0);
  140.                 if(IsDialogEvent(&e) && DialogSelect(&e, &dp, &item) &&
  141.                     progress.win == dp){
  142.  
  143.                     errCode = eUserCancel;
  144.                 }else{
  145.                     DialogOnTopGoEvent(&e);
  146.                 }
  147.             }
  148.         }
  149.         SetGWorld(savePort, saveGD);
  150.     }
  151.     return errCode;
  152. }
  153.  
  154. /* RecordProgress - save position of the progress meter in the preferences
  155.     file.
  156.  */
  157. void RecordProgress(void){
  158.     WindowPtr    savePort;
  159.     Rect        wind;
  160.     Handle        h;
  161.     OSErr        errCode;
  162.  
  163.     if(NIL == progress.win || -1 == prefResFile){
  164.         return;
  165.     }
  166.     GetPort(&savePort);
  167.     SetPort(progress.win);
  168.     wind = qd.thePort->portRect;
  169.     LocalToGlobal(&topLeft(wind));
  170.     LocalToGlobal(&botRight(wind));
  171.     errCode = PtrToHand(&wind, &h, sizeof(Rect));
  172.     if(noErr == errCode){ 
  173.         SavePreferencesResource(h, 'Wind', 128);
  174.         DisposeHandle(h);
  175.     }
  176.     SetPort(savePort);
  177. }
  178.